home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / scrollba.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  6.7 KB  |  270 lines

  1. /*
  2.  * @(#)Scrollbar.java    1.16 95/10/05 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.ScrollbarPeer;
  22.  
  23. /**
  24.  * A Scrollbar component.
  25.  *
  26.  * @version    1.16 10/05/95
  27.  * @author     Sami Shaio
  28.  */
  29. public class Scrollbar extends Component {
  30.   
  31.     /**
  32.      * The horizontal Scrollbar variable.
  33.      */
  34.     public static final int    HORIZONTAL = 0;
  35.  
  36.     /**
  37.      * The vertical Scrollbar variable.
  38.      */
  39.     public static final int    VERTICAL   = 1;
  40.  
  41.     /**
  42.      * The value of the Scrollbar.
  43.      */
  44.     int    value;
  45.  
  46.     /**
  47.      * The maximum value of the Scrollbar.
  48.      */
  49.     int    maximum;
  50.  
  51.     /**
  52.      * The minimum value of the Scrollbar.
  53.      */
  54.     int    minimum;
  55.  
  56.     /**
  57.      * The size of the visible portion of the Scrollbar.
  58.      */
  59.     int    sVisible;
  60.  
  61.     /**
  62.      * The Scrollbar's orientation--being either horizontal or vertical.
  63.      */
  64.     int    orientation;
  65.  
  66.     /**
  67.      * The amount by which the scrollbar value will change when going
  68.      * up or down by a line.
  69.      */
  70.     int lineIncrement = 1;
  71.  
  72.     /**
  73.      * The amount by which the scrollbar value will change when going
  74.      * up or down by a page.
  75.      */
  76.     int pageIncrement = 10;
  77.  
  78.     
  79.     /**
  80.      * Constructs a new vertical Scrollbar.
  81.      */
  82.     public Scrollbar() {
  83.     this(VERTICAL);
  84.     }
  85.  
  86.     /**
  87.      * Constructs a new Scrollbar with the specified orientation.
  88.      * @param orientation either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
  89.      * @exception IllegalArgumentException If an illegal scrollbar orientation is given.
  90.      */
  91.     public Scrollbar(int orientation) {
  92.     switch (orientation) {
  93.       case Scrollbar.HORIZONTAL:
  94.       case Scrollbar.VERTICAL:
  95.         this.orientation = orientation;
  96.         break;
  97.  
  98.       default:
  99.         throw new IllegalArgumentException("illegal scrollbar orientation");
  100.     }
  101.     }
  102.  
  103.     /**
  104.      * Constructs a new Scrollbar with the specified orientation,
  105.      * value, page size,  and minumum and maximum values.
  106.      * @param orientation either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
  107.      * @param value the scrollbar's value
  108.      * @param visible the size of the visible portion of the
  109.      * scrollable area. The scrollbar will use this value when paging up
  110.      * or down by a page.
  111.      * @param minimum the minimum value of the scrollbar
  112.      * @param maximum the maximum value of the scrollbar
  113.      */
  114.     public Scrollbar(int orientation, int value, int visible, int minimum, int maximum) {
  115.     this(orientation);
  116.     setValues(value, visible, minimum, maximum);
  117.     }
  118.  
  119.     /**
  120.      * Create the Scrollbar's peer.  The peer allows us to modify the appearance of the 
  121.      * Scrollbar without changing any of its functionality.
  122.      */
  123.     public synchronized void addNotify() {
  124.     peer = getToolkit().createScrollbar(this);
  125.     super.addNotify();
  126.     }
  127.  
  128.     /**
  129.      * Returns the orientation for this Scrollbar.
  130.      */
  131.     public int getOrientation() {
  132.     return orientation;
  133.     }
  134.  
  135.     /**
  136.      * Returns the current value of this Scrollbar.
  137.      * @see #minimum
  138.      * @see #maximum
  139.      */
  140.     public int getValue() {
  141.     return value;
  142.     }
  143.  
  144.     /**
  145.      * Sets the value of this Scrollbar to the specified value.
  146.      * @param value the new value of the Scrollbar. If this value is
  147.      * below the current minimum or above the current maximum, it becomes the
  148.      * new one of those values, respectively.
  149.      * @see #getValue
  150.      */
  151.     public void setValue(int value) {
  152.     if (value < minimum) {
  153.         value = minimum;
  154.     }
  155.     if (value > maximum) {
  156.         value = maximum;
  157.     }
  158.     if (value != this.value) {
  159.         this.value = value;
  160.         ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  161.         if (peer != null) {
  162.         peer.setValue(value);
  163.         }
  164.     }
  165.     }
  166.  
  167.     /**
  168.      * Returns the minimum value of this Scrollbar.
  169.      * @see #maximum
  170.      * @see #value
  171.      */
  172.     public int getMinimum() {
  173.     return minimum;
  174.     }
  175.  
  176.     /**
  177.      * Returns the maximum value of this Scrollbar.
  178.      * @see #minimum
  179.      * @see #value
  180.      */
  181.     public int getMaximum() {
  182.     return maximum;
  183.     }
  184.  
  185.     /**
  186.      * Returns the visible amount of the Scrollbar.
  187.      */
  188.     public int getVisible() {
  189.     return sVisible;
  190.     }
  191.  
  192.     /**
  193.      * Sets the line increment for this scrollbar. This is the value
  194.      * that will be added (subtracted) when the user hits the line down
  195.      * (up) gadgets.
  196.      */
  197.     void setLineIncrement(int l) {
  198.     lineIncrement = l;
  199.     if (peer != null) {
  200.         ((ScrollbarPeer)peer).setLineIncrement(l);
  201.     }
  202.     }
  203.  
  204.     /**
  205.      * Gets the line increment for this scrollbar.
  206.      */
  207.     int getLineIncrement() {
  208.     return lineIncrement;
  209.     }
  210.  
  211.     /**
  212.      * Sets the page increment for this scrollbar. This is the value
  213.      * that will be added (subtracted) when the user hits the page down
  214.      * (up) gadgets.
  215.      */
  216.     void setPageIncrement(int l) {
  217.     pageIncrement = l;
  218.     if (peer != null) {
  219.         ((ScrollbarPeer)peer).setPageIncrement(l);
  220.     }
  221.     }
  222.  
  223.     /**
  224.      * Gets the page increment for this scrollbar.
  225.      */
  226.     int getPageIncrement() {
  227.     return pageIncrement;
  228.     }
  229.  
  230.     /**
  231.      * Sets the values for this Scrollbar.
  232.      * @param value is the position in the current window.
  233.      * @param visible is the amount visible per page
  234.      * @param minimum is the minimum value of the scrollbar
  235.      * @param maximum is the maximum value of the scrollbar
  236.      */
  237.     public void setValues(int value, int visible, int minimum, int maximum) {
  238.     if (maximum < minimum) {
  239.         maximum = maximum;
  240.     }
  241.     if (value < minimum) {
  242.         value = minimum;
  243.     }
  244.     if (value > maximum) {
  245.         value = maximum;
  246.     }
  247.  
  248.     this.value = value;
  249.     this.sVisible = visible;
  250.     this.minimum = minimum;
  251.     this.maximum = maximum;
  252.     ScrollbarPeer peer = (ScrollbarPeer)this.peer;
  253.     if (peer != null) {
  254.         peer.setValues(value, sVisible, minimum, maximum);
  255.     }
  256.     }
  257.  
  258.     /**
  259.      * Returns the String parameters for this Scrollbar.
  260.      */
  261.     protected String paramString() {
  262.     return super.paramString() +
  263.         ",val=" + value +
  264.         ",vis=" + visible +
  265.         ",min=" + minimum +
  266.         ",max=" + maximum +
  267.         ((orientation == VERTICAL) ? ",vert" : ",horz");
  268.     }
  269. }
  270.